package com.bdth.mixapi.api.controller.traffic;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdth.mixapi.api.entity.Onlinerate;
import com.bdth.mixapi.api.service.OnlinerateService;
import com.bdth.mixapi.api.vo.CommonList;
import com.bdth.mixapi.api.vo.CommonPage;
import com.bdth.mixapi.utils.DateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LGP
 * @since 2019-04-15
 */
@Api("车辆在线率")
@RestController
@RequestMapping("/traffic")
public class OnlinerateController {
    @Autowired
    OnlinerateService onlinerateService;

    @ApiOperation(value="车辆所有在线率列表")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "depName", dataType = "String", required = false, value = "查询名称", defaultValue = ""),
    })
    @RequestMapping(value = "onlinerate/list", method = RequestMethod.GET)
    public CommonList<Onlinerate> list(@RequestParam(value = "depName",required=false) String depName){
        //查询有数据的最后一天
        QueryWrapper<Onlinerate> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("StatisticDate");
        wrapper.last("limit 1");
        List<Onlinerate> list = onlinerateService.list(wrapper);

        if(list!=null && list.size()>0) {
            QueryWrapper<Onlinerate> queryWrapper = new QueryWrapper<>();
            if (depName != null) {
                queryWrapper.like("depName", depName);
            }
            //2天前的统计数据
            queryWrapper.like("StatisticDate", list.get(0).getStatisticDate());
            queryWrapper.orderByDesc("OnlineRate");
            List<Onlinerate> onlinerateList = onlinerateService.list(queryWrapper);
            return CommonList.success(onlinerateList);
        }else{
            return CommonList.fail("没有数据");
        }
    }

    @ApiOperation(value="车辆在线率列表分页")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "depName", dataType = "String", required = false, value = "查询名称", defaultValue = ""),
            @ApiImplicitParam(paramType = "query", name = "now", dataType = "int", required = false, value = "当前页码", defaultValue = "1"),
            @ApiImplicitParam(paramType = "query", name = "rows", dataType = "int", required = false, value = "每页条数", defaultValue = "20")
    })
    @RequestMapping(value = "onlinerate/page", method = RequestMethod.GET)
    public CommonPage<Onlinerate> page(@RequestParam(value = "depName",required=false) String depName,@RequestParam Integer now, @RequestParam Integer rows){
        //查询有数据的最后一天
        QueryWrapper<Onlinerate> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("StatisticDate");
        wrapper.last("limit 1");
        List<Onlinerate> list = onlinerateService.list(wrapper);
        if(list!=null && list.size()>0) {
            QueryWrapper<Onlinerate> queryWrapper = new QueryWrapper<>();
            if (depName != null) {
                queryWrapper.like("depName", depName);
            }
            //2天前的统计数据
            queryWrapper.like("StatisticDate", list.get(0).getStatisticDate());
            Page<Onlinerate> page = new Page<>(now == null ? 1 : now, rows == null ? 20 : rows);
            onlinerateService.page(page, queryWrapper);
            CommonPage<Onlinerate> result = new CommonPage<Onlinerate>(page.getRecords(), page.getTotal());
            return result;
        }else{
            return new CommonPage<Onlinerate>(new ArrayList<>(), 0);
        }
    }

    @ApiOperation(value="车辆在线率TOP10")
    @RequestMapping(value = "onlinerate/rateTop10", method = RequestMethod.GET)
    public CommonList<Onlinerate> rateTop10(){
        //查询有数据的最后一天
        QueryWrapper<Onlinerate> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("StatisticDate");
        wrapper.last("limit 1");
        List<Onlinerate> list = onlinerateService.list(wrapper);
        if(list!=null && list.size()>0) {
            QueryWrapper<Onlinerate> queryWrapper = new QueryWrapper<>();
            //2天前的统计数据
            queryWrapper.like("StatisticDate", list.get(0).getStatisticDate());
            queryWrapper.last("limit 10");
            queryWrapper.orderByDesc("OnlineRate");
            List<Onlinerate> onlinerateList = onlinerateService.list(queryWrapper);
            return CommonList.success(onlinerateList);
        }else{
            return CommonList.fail("没有数据");
        }
    }

    @ApiOperation(value="车辆数量TOP10")
    @RequestMapping(value = "onlinerate/numTop10", method = RequestMethod.GET)
    public CommonList<Onlinerate> numTop10(){
        //查询有数据的最后一天
        QueryWrapper<Onlinerate> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("StatisticDate");
        wrapper.last("limit 1");
        List<Onlinerate> list = onlinerateService.list(wrapper);
        if(list!=null && list.size()>0) {
            QueryWrapper<Onlinerate> queryWrapper = new QueryWrapper<>();
            //2天前的统计数据
            queryWrapper.like("StatisticDate", list.get(0).getStatisticDate());
            queryWrapper.last("limit 10");
            queryWrapper.orderByDesc("DeviceNum");
            List<Onlinerate> onlinerateList = onlinerateService.list(queryWrapper);
            return CommonList.success(onlinerateList);
        }else{
            return CommonList.fail("没有数据");
        }
    }

}
